/*
 * scumix.swat.spark.StepButton
 * 步进按钮
 *
 * author: scumyang
 * create: 2009-09-16
 * update: 2009-09-16
 * notes : 需导入 scumix.swat.swat3.js
 */

/*
用法:
	
1.声明变量:
var t = null;

2.初始化:
$.pageReady(function()
{
	t = new scumix.swat.spark.StepButton('XStepButton',{min:0,max:100,value:10,step:1,bind:'edStep',repeatDelay:500,repeatRate:20});
	t.onChange=function(S)
	{
		document.title = S.value;
	}

});

StepButton( 容器, 构造参数 );
构造参数:
min		:最小值
max		:最大值
value	:当前值
step	:递增/递减步长
bind	:绑定元素(用于显示, 其 value 属性将绑定)
repeatDelay	: 持续动作前的延时(ms)
repeatRate	: 持续动作频率(ms)

*/

scumix.swat.spark.StepButton = $.newClass();
scumix.swat.spark.StepButton.prototype =
{
	constructor: function( eContainer, P )
	{
		// global id
		this.id = $.makeGlobalId( 'SxStepButton' );
		this.ElName = this.id;
		
		// Constraction params.
		this.minValue = P.min || 0;
		this.maxValue = P.max || 100;
		this.value = P.value || 0;
		this.step = P.step || 1;
		this.repeatDelay = $.defaultValue( P.repeatDelay, 500 );
		this.repeatRate = $.defaultValue( P.repeatRate, 25 );

		// Internal vars.
		this.autoAction = 0;

		// Chaining DOM objects
		this.EContainer = $E(eContainer) || null;
		this.EFrame = null;
		this.EUpButton = null;
		this.EDownButton = null;
		this.EBind = $E(P.bind) || null;

		// events and handler
		this.pAutoChangeTimer = null;
		this.onChange = null;

		// init view
		this.initView( eContainer, P );
	},

	// init view
	initView: function( eContainer, P )
	{
		// main bkg container
		var oFrame = $.createElement({type:'div', id:this.ElName, css:'', attr:{className:'SxStepButton'}});
		this.EFrame = oFrame;

			// up-button
			var o = $.createElement({type:'div', id:this.ElName+'_Up', css:'', attr:{className:'SxStepButton_Up',title:'增加'}});
				o.onmouseover = function(){ this.className = 'SxStepButton_Up_h' }
				o.onmouseout = function(){ this.className = 'SxStepButton_Up' }
				o.onmousedown = this.DoUpButtonDown.bind(this);
				o.onmouseup = this.DoUpButtonUp.bind(this);
			this.EUpButton = o;
			oFrame.appendChild(o);

			// down-button
				o = $.createElement({type:'div', id:this.ElName+'_Down', css:'', attr:{className:'SxStepButton_Down',title:'减少'}});
				o.onmouseover = function(){ this.className = 'SxStepButton_Down_h' }
				o.onmouseout = function(){ this.className = 'SxStepButton_Down' }
				o.onmousedown = this.DoDownButtonDown.bind(this);
				o.onmouseup = this.DoDownButtonUp.bind(this);
			this.EDownButton = o;
			oFrame.appendChild(o);

		// add to DOM
		if( this.EContainer ) this.EContainer.appendChild( oFrame );

		// update bind value
		if( this.EBind ) this.EBind.value = this.value;
	},

	updateBind: function( E )
	{
		if( this.EBind ) this.EBind.value = this.value;
		if( this.EBind.onchange ) this.EBind.onchange( E );
	},

	setValue: function( N )
	{
		if( N < this.minValue ) N = this.minValue;
		if( N > this.maxValue ) N = this.maxValue;
		this.value = N;
		this.updateBind();
	},

	// increase value
	incValue: function( E )
	{
		this.value += this.step;
		if( this.value > this.maxValue ) this.value = this.maxValue;
		this.updateBind( E );
	},

	// decrease value
	decValue: function( E )
	{
		this.value -= this.step;
		if( this.value < this.minValue ) this.value = this.minValue;
		this.updateBind( E );		
	},

	// auto change value routine
	_autoChange: function()
	{
		switch( this.autoAction )
		{
			case 1: this.incValue(); break;
			case 2: this.decValue(); break;
			default: return;
		}

		// callback
		if( this.onChange ) this.onChange( this );
	},

	// start timer
	StartAutoChange: function()
	{
		this.pAutoChangeTimer = setInterval( function(){ this._autoChange() }.bind(this), this.repeatRate );
	},

	// events
	DoUpButtonDown: function( E )
	{
		if( $.browser.msie ) this.EUpButton.setCapture();
		this.autoAction = 1;
		this.incValue();
		var Q = this;
		setTimeout( function(){Q.StartAutoChange()}, this.repeatDelay );
	},

	DoUpButtonUp: function( E )
	{
		if( $.browser.msie ) this.EUpButton.releaseCapture();
		this.autoAction = 0;
		if( this.pAutoChangeTimer ) clearInterval( this.pAutoChangeTimer );
	},

	DoDownButtonDown: function( E )
	{
		if( $.browser.msie ) this.EDownButton.setCapture();
		this.autoAction = 2;
		this.decValue();
		var Q = this;
		setTimeout( function(){Q.StartAutoChange()}, this.repeatDelay );
	},

	DoDownButtonUp: function( E )
	{
		if( $.browser.msie ) this.EDownButton.releaseCapture();
		this.autoAction = 0;
		if( this.pAutoChangeTimer ) clearInterval( this.pAutoChangeTimer );
	}
}
